package com.esiran.payadmin.controller;

import com.esiran.payadmin.entity.Resp;
import com.esiran.payadmin.exception.APIRespException;
import com.esiran.payadmin.model.Order;
import com.esiran.payadmin.service.OrderService;
import com.esiran.payadmin.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping
public class IndexController {
    private final OrderService orderService;

    @Autowired
    public IndexController(OrderService orderService) {
        this.orderService = orderService;
    }
    
    @GetMapping("test")
    public String test(){
        return "test";
    }

    @PostMapping("/order")
    public String order(
            @RequestParam("app_id") String appId,
            @RequestParam("goods_name") String goodsName,
            @RequestParam("amount") Double amount,
            @RequestParam("extra_id") String extraId,
            @RequestParam(value = "return_url",required = false) String returnUrl,
            @RequestParam(value = "notify_url",required = false) String notifyUrl
    ) throws APIRespException {
        Order existsOrder = orderService.getOrderByMerchExtraNum(1L,extraId);
        if (existsOrder != null){
            return "redirect:/order/".concat(existsOrder.getNum());
        }
        Order order = orderService.create4merchant(appId,goodsName,amount,extraId,notifyUrl);
        String redirectUrl = "redirect:/cashier/pay?order_num=".concat(order.getNum());
        if (returnUrl!=null){
            redirectUrl += "&return_url=".concat(returnUrl);
        }
        return redirectUrl;
    }
    @GetMapping("/order/{num}")
    public String payment(
            @PathVariable String num,
            ModelMap modelMap) throws APIRespException {
        Order order = orderService.getOrderByNum(num);
        if (order == null){
            throw new APIRespException("订单不存在",-1009);
        }
        if (order.getState() == 0){
            return "redirect:/cashier/pay?order_num=".concat(order.getNum());
        }
        modelMap.addAttribute("order",order);
        return "user/order_info";
    }
    @GetMapping("/cashier/pay")
    public String cashier(
            @RequestParam("order_num") String orderNum,
            @RequestParam(value = "return_url",required = false) String returnUrl,
            HttpServletResponse response,
            ModelMap modelMap) throws APIRespException, IOException {
        Order order = orderService.getOrderByNum(orderNum);
        if (order == null){
            throw new APIRespException("订单不存在或已失效",-1003);
        }
        if (!order.getState().equals(0L)){
            response.sendRedirect("redirect:/order/".concat(orderNum));
            return null;
        }
        if (returnUrl!=null){
            modelMap.addAttribute("returnUrl",returnUrl);
        }
        modelMap.addAttribute("order",order);
        return "user/payment";
    }
    @PostMapping("/cashier/pay")
    @ResponseBody
    public Resp<Map<String,Object>> doPay(
            @RequestParam("order_num") String orderNum,
            @RequestParam("pay_way") Integer payWay,
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap) throws APIRespException, IOException {
        Order order = orderService.getOrderByNum(orderNum);
        if (order == null){
            throw new APIRespException("订单不存在或已失效",-1003);
        }
        if (!order.getState().equals(0L)){
            throw new APIRespException("该订单已支付，不能重复发起支付",-1003);
        }
        Map<String,Object> resp = new HashMap<>();
        String qrCodeData = orderService.payOrderByQRCode(orderNum,payWay);
        resp.put("qrcode_data",qrCodeData);
        resp.put("pay_way",payWay);
        return Resp.create(0,resp);
    }
}
